<?php
/*
	[Office 515158] (C) 2009-2012 天生创想 Inc.
	$Id: mod_registration 1209087 2012-01-08 08:58:28Z baiwei.jiang $
*/
(!defined('IN_TOA') || !defined('IN_ADMIN')) && exit('Access Denied!');
empty($do) && $do = 'list';
$_check['ischeck']='  ui-tab-trigger-item-current';
if ($do == 'list') {
	//列表信息 
	$wheresql = '';
	$page = max(1, getGP('page','G','int'));
	$pagesize = $_CONFIG->config_data('pagenum');
	$offset = ($page - 1) * $pagesize;
	$url = 'admin.php?ac='.$ac.'&fileurl='.$fileurl.'';
	$num = $db->result("SELECT COUNT(*) AS num FROM ".DB_TABLEPRE."registration WHERE uid='".$_USER->id."' and date!='".get_date('Y-m-d',PHP_TIME)."' and unionid='".union_id."'  ORDER BY id desc");
    $sql = "SELECT * FROM ".DB_TABLEPRE."registration WHERE uid='".$_USER->id."' and date!='".get_date('Y-m-d',PHP_TIME)."' and unionid='".union_id."'  ORDER BY id desc LIMIT $offset, $pagesize";
	$result = $db->fetch_all($sql);
	//生成打卡记录
	$user = $db->fetch_one_array("SELECT * FROM ".DB_TABLEPRE."registration  WHERE date='".get_date('Y-m-d',PHP_TIME)."' and unionid='".union_id."' ");
	if($user['id']==''){
		get_union_add();
		$registration = array(
				'name' => GET_INC_USER_NAME($_USER->id),
				'uid' => $_USER->id,
				'date' => get_date('Y-m-d',PHP_TIME),
				'unionid' => union_id
		);
		insert_db('registration',$registration);
	}
	//获取当次打卡信息
	$rowdata = $db->fetch_one_array("SELECT * FROM ".DB_TABLEPRE."registration  WHERE date='".get_date('Y-m-d',PHP_TIME)."' and unionid='".union_id."' ");
	//提示
	if(get_date('H',PHP_TIME)>12 && get_date('H',PHP_TIME)<$_CONFIG->config_data('ework') && $_GET['type']!='1' && $rowdata['enddate']==''){
		show_msg_type('还没有到下班时间，您确定要执行早退吗！', 'admin.php?ac='.$ac.'&fileurl='.$fileurl.'&type=1');
	}
	//上班打卡
	if (get_date('H',PHP_TIME)<12 && $rowdata['startdate']=='' ){
		get_union_add();
		if(get_date('H',PHP_TIME)<$_CONFIG->config_data('swork')){
			$startyype='0';
		}else{
			$startyype='1';
		}
		$registration = array(
			'startdate' => get_date('Y-m-d H:i:s',PHP_TIME),
			'startyype' => $startyype
		);
		update_db('registration',$registration, array('id' => $rowdata['id']));
		show_msg('上班在线签到成功！', 'admin.php?ac='.$ac.'&fileurl='.$fileurl.'');
	}
	//下班打卡
	if (get_date('H',PHP_TIME)>12 && $rowdata['enddate']==''){
		get_union_add();
		if(get_date('H',PHP_TIME)>=$_CONFIG->config_data('ework')){
			$endtype='0';
		}else{
			$endtype='1';
		}
		$registration = array(
			'enddate' => get_date('Y-m-d H:i:s',PHP_TIME),
			'endtype' => $endtype
		);
		update_db('registration',$registration, array('id' => $rowdata['id']));
		show_msg('下班在线签到成功！', 'admin.php?ac='.$ac.'&fileurl='.$fileurl.'');
	}
	
	include_once('template/registrationlist.php');

}elseif ($do == 'add') {
		get_union_add();
		$startnote = check_str(getGP('startnote','P'));
		$endnote = check_str(getGP('endnote','P'));
		$registration = array(
			'startnote' => $startnote,
			'endnote' => $endnote
		);
		update_db('registration',$registration, array('id' => $_POST['id']));
		show_msg('成功更新考勤原因！', 'admin.php?ac='.$ac.'&fileurl='.$fileurl.'');
}
?>